package com.xcity.game.player.robot;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import com.xcity.game.buildv2.BuildManagerV2;
import com.xcity.game.buildv2.BuildV2;
import com.xcity.game.buildv2.BuildV2Part;
import com.xcity.game.buildv2.template.BuildTemplateV2;
import com.xcity.game.npc.NpcTemplate;
import com.xcity.game.player.robot.RobotTemplate.BuildingConfig;

import naga.x.App;
import naga.x.game.template.TemplateService;
import naga.x.game.time.TimeUpdater;

public class RobotBuild extends BuildV2 {

	private BuildingConfig config;
	
	private List<NpcTemplate> npcs;
	
	public RobotBuild(BuildManagerV2 vm, BuildTemplateV2 template, BuildingConfig config) {
//		super(vm, template);
		//---super---
		this.vm = vm;
		this.template = template;
		this.id = template.getId();
		this.state = State.CAN_BUILD;//BuildUtils.isUnlock(vm.getOwner(), template) ? State.CAN_BUILD : State.LOCKED;
		this.parts = new BuildV2Part[PART_COUNT];
		for (int i = 0; i < PART_COUNT; i++) {
			this.parts[i] = new BuildV2Part(i, template.getPart(i));
		}
		this.createTime = TimeUpdater.getInstance().now();
		this.lastTakeMoneyTime = -1;
		this.lastTakeMoney = -1;
		this.putItemId = -1;
		this.lastTakeExpTime = -1;
		this.lastStartMakeMoneyTime = -1;
		this.lastPutEmployeeTime = new long[MAX_SLOT];
		//---super---
		
		this.config = config;
	}
	
	public List<NpcTemplate> getNpcs() {
		if (npcs == null) {
			int[] npcIds = config.npcIds;
			if (state == State.BUILT && npcIds != null) {
				npcs = new ArrayList<NpcTemplate>(npcIds.length);
				TemplateService service = App.getApp().getServiceManager().get(TemplateService.class);
				for (int npcId : npcIds) {
					npcs.add(npcId != 0 ? service.get(NpcTemplate.class, npcId) : null);
				}
			} else {
				npcs = Collections.emptyList();
			}
		}
		return npcs;
	}
	
	
}
